package resman.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;

import resman.bean.PersonalBean;
import resman.bean.ResumeBean;
import resman.database.DatabaseHandler;
import resman.database.TransSession;
import resman.model.ActionServlet;

@SuppressWarnings("serial")
@WebServlet("/PersonalServlet")
public class PersonalServlet extends ActionServlet
{
	
	@Override
	protected void process(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException
	{
		TransSession tsession = DatabaseHandler.openSession(true);
		Session session = tsession.getSession();
		
		ResumeBean resume = getResume(request);
		PersonalBean personal = resume.getPersonal();
		
		if (personal == null)
		{
			personal = new PersonalBean();
			resume.setPersonal(personal);
			personal.setProperties(request.getParameterMap());
			personal.setResume(resume);
			session.save(personal);
		}
		else
		{
			personal.setProperties(request.getParameterMap());
			session.merge(personal);
		}
		
		tsession.close();
		dispatchTo("views/personal_view.jsp", request, response);
	}

}
